package com.ruoyi.web.controller.app;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.TokenInfoVO;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.dto.RegisterDto;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 登录验证
 *
 * @author ruoyi
 */

@Api(tags = "用户端:登录/注册相关接口")
@RestController
@RequestMapping("/app")
public class LoginController extends BaseController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;
    @Resource
    private ISysUserService userService;

    @Resource
    private RedisCache redisCache;

    /**
     * 登录方法
     * 每秒限流5次
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @RateLimiter(time = 2, count = 5)
    @RequestMapping("/login")
    @ApiOperation("账号密码登录")
    public ResponseResult login( @RequestBody  LoginBody loginBody) {

        System.out.println(JSONUtil.toJsonStr(loginBody));
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        LoginUser token = loginService.login1(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        TokenInfoVO token1 = new TokenInfoVO();
        token1.setAccessToken(token.getToken());
        token1.setExpiresIn(100000);
        ResponseResult responseResult = this.setSuccessResult(token1);
        return responseResult;
    }

    @RateLimiter(time = 2, count = 5)
    @PostMapping("/pwd/login")
    @ApiOperation("账号密码登录")
    public ResponseResult pwdLogin(@Valid @RequestBody LoginBody loginBody) {

        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        ResponseResult responseResult = this.setSuccessResult(token);
        return responseResult;
    }

    @PostMapping("/code/login")
    @ApiOperation("验证码登录")
    public ResponseResult codeLogin(@RequestBody LoginBody loginBody) {

        // 生成令牌
        String token = loginService.codeLogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return this.setSuccessResult(token);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public ResponseResult register(@RequestBody RegisterDto registerDto) {

        // 生成令牌
        String token = loginService.phoneRegister(registerDto);
        return this.setSuccessResult(token);
    }

    @Resource
    private CashHistoryService cashHistoryService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @ApiOperation("获取用户信息")
    public ResponseResult getInfo() {

        SysUser user = userService.getById(this.getUserId());
        return this.setSuccessResult(user);
    }

}
